<?php
/**
 * Created by PhpStorm.
 * User: alinejun
 * Date: 2019/1/7
 * Time: 22:31
 */
namespace app\common\model;

/**
 * 分类模型
 */
class Category extends ModelBase
{
    public function search($where = [], $field = true, $order = '', $paginate = 0)
    {
        $list = $this->getList($where, $field, $order, $paginate);
        return $list;
    }
    public function del($where = [], $field = '', $value = ''){
        return  $this->setFieldValue($where,$field,$value);
    }

    public function edit($where = [], $data = [])
    {
        return $this->updateInfo($where,$data);
    }

    public function add($data = [], $where = [])
    {
        return $this->setInfo($data,$where);
    }

    public function editCategory($data)
    {

        $validate_result = $this->validateCategory->scene('edit')->check($data);

        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateCategory->getError()];
        }

        $url = url('index');
        $result = $this->modelCategory->setInfo($data);
        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '分类操作成功', $url] : [RESULT_ERROR, $this->modelCategory->getError()];
    }


    public function getCategoryById($where=[])
    {
        $list = $this->getInfo($where);
        return $list->toArray();
    }
}